from typing import Union

from fastapi import FastAPI, Query
import uvicorn

app = FastAPI()


# 1.默认值设置，和参数接⼝描述
#  description是 Query中的⼀个字段，⽤来描述该参数；
@app.get('/item1')
async def item1(q: Union[str, None] = Query(default=None, description="参数描述")):
    return {'q': q}


# 2.字符串⻓度校验
# max_length和min_length仅可⽤于str类型的参数。

@app.get('/item2')
async def item2(q: Union[str, None] = Query(description="这个是测传参最大和最小的", max_length=200, min_length=2)):
    return {'q': q}


# 3.正则表达式校验
@app.get('/item3')
async def item3(q: Union[str, None] = Query(description="这个是正则表达式", regex='^liao$')):
    return {'q': q}


# 4.数值⼤⼩校验
# gt：⼤于（greater than）
# ge：⼤于等于（greater than or equal）
# lt：⼩于（less than）
# le：⼩于等于（less than or equal
@app.get('/item4/')
async def item4(q: int = Query(description="这是比较大小", gt=2, lt=10)):
    results = {'item_id': q}
    return results


if __name__ == '__main__':
    uvicorn.run(app, host='127.0.0.1', port=8000)
